//
//  ChangePasswordViewController.m
//  SecretFolder
//
//  Created by Deepak Kumar on 24/06/12.
//  Copyright 2012 Frog. All rights reserved.
//

#import "ChangePasswordViewController.h"


@implementation ChangePasswordViewController
@synthesize mTableView;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(changePasswordClicked:)];
    
	self.navigationItem.title = @"Change Password";
    NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica" size:17],UITextAttributeFont, nil];
    self.navigationController.navigationBar.titleTextAttributes = size;

}
- (void) changePasswordClicked:(id)sender
{
	if (![oldPasswordTextField.text length] || ![newPasswordTextField.text length] || ![reEnterNewPasswordTextField.text length]) {
		// Please fill in your old and new passwords appropriately
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please fill in your old and new passwords appropriately." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
		[alert show]; // Please fill in all three fields.
	}
	else if([newPasswordTextField.text isEqualToString: oldPasswordTextField.text] == YES)
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your new password is identical to current one. Please, select new password." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
		[alert show];
	}
	else if([newPasswordTextField.text isEqualToString: reEnterNewPasswordTextField.text] == YES)
	{
		[self changePassword];
	}
	else
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The two passwords you entered do not match." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
		[alert show];
	}
}

- (void) changePassword
{
	NSString *tOldPwd = [[NSUserDefaults standardUserDefaults] objectForKey:kPasswordText];
	
	if ([tOldPwd isEqualToString:oldPasswordTextField.text]) {
		
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your password has been changed successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
		[alert show];
		
		[[NSUserDefaults standardUserDefaults] setObject:newPasswordTextField.text forKey:kPasswordText];
		[self.navigationController popViewControllerAnimated:YES];

	}
	else {
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Old password is not matched." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
		[alert show];
	}

}
#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 3;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		[cell.textLabel setFont:[UIFont systemFontOfSize:12.0]];
	}
	
	[cell.textLabel setTextColor:[UIColor colorWithRed:(97.0f/255.0f) green:(90.0f/255.0f) blue:(90.0f/255.0f) alpha:1]];
    
	// Configure the cell...
	if (indexPath.row == 0) {
		cell.textLabel.text = @"Old Password";
		
		if (oldPasswordTextField == nil) {
			oldPasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 14, 150, 20)];
			oldPasswordTextField.delegate = self;
			oldPasswordTextField.secureTextEntry = YES;
			oldPasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
			// Border Style None
			//[oldPasswordTextField setBorderStyle:UITextBorderStyleRoundedRect];
			oldPasswordTextField.font = [UIFont systemFontOfSize:12.0];
			//oldPasswordTextField.placeholder = @"Enter old password";
			oldPasswordTextField.returnKeyType = UIReturnKeyNext;
			oldPasswordTextField.tag = indexPath.row;
			oldPasswordTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
			
			[cell addSubview:oldPasswordTextField];
			
			[oldPasswordTextField becomeFirstResponder];
		}
	}
	else if (indexPath.row == 1) {
		cell.textLabel.text = @"New Password";
		if (newPasswordTextField == nil) {
			newPasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 14, 150, 20)];
			newPasswordTextField.delegate = self;
			newPasswordTextField.secureTextEntry = YES;
			newPasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
			// Border Style None
			//[newPasswordTextField setBorderStyle:UITextBorderStyleRoundedRect];
			newPasswordTextField.font = [UIFont systemFontOfSize:12.0];
			//newPasswordTextField.placeholder = @"Enter new password";
			newPasswordTextField.returnKeyType = UIReturnKeyNext;
			newPasswordTextField.tag = indexPath.row;
			newPasswordTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
			
			[cell addSubview:newPasswordTextField];
		}
		
	}
	else if (indexPath.row == 2) {
		cell.textLabel.text = @"Re-enter new password";
		
		if (reEnterNewPasswordTextField == nil) {
			reEnterNewPasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 14, 150, 20)];
			reEnterNewPasswordTextField.delegate = self;
			reEnterNewPasswordTextField.secureTextEntry = YES;
			reEnterNewPasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
			// Border Style None
			//[reEnterNewPasswordTextField setBorderStyle:UITextBorderStyleRoundedRect];
			reEnterNewPasswordTextField.font = [UIFont systemFontOfSize:12.0];
			//reEnterNewPasswordTextField.placeholder = @"Re enter new password";
			reEnterNewPasswordTextField.returnKeyType = UIReturnKeyDone;
			reEnterNewPasswordTextField.tag = indexPath.row;
			reEnterNewPasswordTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
			
			[cell addSubview: reEnterNewPasswordTextField];
		}
	}
    
	cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
	return cell;
}

#pragma mark -
#pragma mark text field delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
	[self.mTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:textField.tag inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *) textField 
{
	if (oldPasswordTextField == textField) {
		[newPasswordTextField becomeFirstResponder];
	}
	else if (newPasswordTextField == textField) {
		[reEnterNewPasswordTextField becomeFirstResponder];
	}
	else if (reEnterNewPasswordTextField == textField) {
		[textField resignFirstResponder];
	}
	
    return YES;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Override to allow orientations other than the default portrait orientation.
    return UIInterfaceOrientationIsLandscape(interfaceOrientation)? YES : NO;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.mTableView = nil;
}


- (void)dealloc {
	
	if (oldPasswordTextField) {
		oldPasswordTextField = nil;
	}
    
	if (newPasswordTextField) {
		newPasswordTextField = nil;
	}
	
	if (reEnterNewPasswordTextField) {
		reEnterNewPasswordTextField = nil;
	}
	
}


@end
